
MASTER_IP=`docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' redis-master`

echo "MASTER_IP："$MASTER_IP
if [ -z "$MASTER_IP" ]; then
    echo "主节点IP未查到，可能未运行，请确认"
    exit
fi


# ================ source 调用本脚本时可用变量 ==============
# $PORT=26381
# $REDIS_HOME=/opt/module/redis/26381
# $REDIS_DATA=/opt/module/redis/26381/data
# $REDIS_CONF=/opt/module/redis/26381/redis.conf
# $REDIS_LOG=/opt/module/redis/26381/redis.log
# ====================================================
REDIS_SENTINEL_HOME=/opt/module/redis/sentinel
mkdir -p $REDIS_SENTINEL_HOME
# 创建三份数据目录
PORT_ARRAY=(1 2 3)
for num in ${PORT_ARRAY[@]}; do
#生成数据目录
touch $REDIS_SENTINEL_HOME/sentinel$num.conf
# 覆盖sentinel配置
sudo cat >$REDIS_SENTINEL_HOME/sentinel$num.conf<<EOF
port 26379
dir /tmp
sentinel monitor mymaster $MASTER_IP 6379 2
sentinel auth-pass mymaster 123456
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
EOF
chmod 666 $REDIS_SENTINEL_HOME/sentinel$num.conf
done

# 一主两从
docker-compose up -d

echo "==>> 客户端命令：docker exec -it redis-sentinel-1  /usr/local/bin/redis-cli -p 26379"
# docker-compose  logs -f
docker-compose  logs -f